#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/9/25  20:37
# @Author: 余浪人
# @email: yulangren520@gmail.com

import IPy, json
from apps.control import sys_bp
from flask import jsonify, request, render_template
from apps.lib.public import redis_
from apps.jinja2 import ipAddress
from flask_login import login_required
@sys_bp .route ('/ip_frequency',endpoint ='ipFrequency',methods =['GET','DELETE','EDIT'])
@login_required
def ipFrequency ():
    O0O00O000O00OOOO0 =redis_ (2 )
    if request .method =='DELETE':
        O0O00O000O00OOOO0 .delete (request .form .get ('uid',''))
        return jsonify ({"status":0 ,'msg':"删除成功"})
    elif request .method =='EDIT':
        O00O0OO0OOO00OOOO =redis_ (4 )
        O000OOO0OO0OOO0O0 =request .form .get ('data','')
        OO000O000O0O00O0O =O0O00O000O00OOOO0 .get (O000OOO0OO0OOO0O0)
        if OO000O000O0O00O0O:
            OO0O00000O0OOOOOO =json .loads (OO000O000O0O00O0O)
            OO0O00000O0OOOOOO['address']=ipAddress (O000OOO0OO0OOO0O0)
            O0O00O000O00OOOO0.delete (O000OOO0OO0OOO0O0)
        else :
            OO0O00000O0OOOOOO ={"IP":O000OOO0OO0OOO0O0,"address":ipAddress (O000OOO0OO0OOO0O0)}
        O00O0OO0OOO00OOOO .set (O000OOO0OO0OOO0O0 ,json .dumps (OO0O00000O0OOOOOO))
        return jsonify ({"status":0 ,'msg':"添加成功"})
    OO0OOOO0000O00000 =O0O00O000O00OOOO0 .keys ()
    O0000O00OOO0O0O00 =[]
    for O0O00O00O0000O0O0 in OO0OOOO0000O00000 :
        OOOO00OO0OOOOO0O0 =O0O00O000O00OOOO0 .get (O0O00O00O0000O0O0 )
        if OOOO00OO0OOOOO0O0 and O0O00O000O00OOOO0 .ttl (O0O00O00O0000O0O0 )>=3 :
            O0000O00OOO0O0O00 .append (json .loads (OOOO00OO0OOOOO0O0 ))
    return render_template ('system_template/ipFrequency_list.html',data_list =O0000O00OOO0O0O00 )
@sys_bp .route ('/ipForbid',endpoint ='ipForbid',methods =['GET','DELETE','POST'])
@login_required
def ipForbid ():
    O00OO0OOO00OOOO0O =redis_ (4)
    if request .method =='DELETE':
        O00OO0OOO00OOOO0O .hdel ('forbid_ip',request .form .get ('data'))
        return jsonify ({"status":0 ,'msg':"移出成功"})
    elif request .method =='POST':
        O00OO0O0O000O0OOO =request .form .get ('data')
        try :
            IPy .IP (O00OO0O0O000O0OOO)
            O00OO0OOO00OOOO0O .hset ('forbid_ip',O00OO0O0O000O0OOO ,json .dumps ({"IP":O00OO0O0O000O0OOO ,"address":ipAddress (O00OO0O0O000O0OOO )}))
        except :
            return jsonify ({"status":1 ,'msg':"请输入合法IP地址"})
        return jsonify ({"status":0 ,'msg':"添加成功"})
    OO0OO000OOOO0OOOO =[json .loads (O00OO0OOO00OOOO0O .hget ('forbid_ip',OOO0O0O0O00000OO0 ))for OOO0O0O0O00000OO0 in O00OO0OOO00OOOO0O .hkeys ('forbid_ip')[::-1 ][:50 ]]
    return render_template ('system_template/ipForbid_list.html',data_list =OO0OO000OOOO0OOOO )
@sys_bp .route ('/ipFrozen',endpoint ='ipFrozen',methods =['GET','POST','DELETE'])
def ipFrozen ():
    O000O0O00O0O0O0OO =redis_ (3)
    if request .method =='POST':
        OO000OO0000OO0000 =redis_ (4)
        OOO00OO000000O0O0 =request .form .get('data')
        try :
            IPy.IP (OOO00OO000000O0O0 )
            OO00OOO0OOOO00OOO =OO000OO0000OO0000 .hset ('forbid_ip',OOO00OO000000O0O0 ,json .dumps ({"IP":OOO00OO000000O0O0 ,"address":ipAddress (OOO00OO000000O0O0 )}))
            if OO00OOO0OOOO00OOO :O000O0O00O0O0O0OO .delete (OOO00OO000000O0O0 )
        except :
            return jsonify ({"status":1 ,'msg':"请输入合法IP地址"})
        return jsonify ({"status":0 ,'msg':"添加成功"})
    elif request .method =='DELETE':
        OOO00OO000000O0O0 =request .form .get ('data')
        try :
            IPy .IP (OOO00OO000000O0O0)
            O000O0O00O0O0O0OO .delete (OOO00OO000000O0O0)
        except :
            return jsonify ({"status":1 ,'msg':"请输入合法IP地址"})
        return jsonify ({"status":0 ,'msg':"解除限制成功"})
    O0O0OO000O00OOO0O =O000O0O00O0O0O0OO .keys ()
    O000000OO00O000OO =[{"IP":json .loads (O000O0O00O0O0O0OO .get (O0O0O0O0O00O0O0OO ))}for O0O0O0O0O00O0O0OO in O0O0OO000O00OOO0O]
    return render_template ('system_template/ipFrozen_list.html',data_list =O000000OO00O000OO)
